﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;

namespace Texfina.Core.Web
{
    [ToolboxData("<{0}:esdFilterText Runat=server></{0}:esdFilterText>")]
    public class esdFilterText : WebControl
    {
        //private ScriptManager sm;

        [DefaultValue(null)]
        public DataTable DataSourceListToken
        {
            get
            {
                if (this.ViewState["_dtlt"] == null)
                    return null;
                else
                    return (DataTable)this.ViewState["_dtlt"];
            }
            set { this.ViewState["_dtlt"] = value; }
        }


        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void RenderContents(HtmlTextWriter writer)
        {

            TextWriter RenderHtml = new StringWriter();
            RenderHtml.Write("<input type=\"text\" id=\"" + this.ID + "\" name=\"blah\" />");
            writer.Write(RenderHtml.ToString());

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(" $(function () {");
            sb.AppendLine("    $(\"#" + this.ID + "\").tokenInput(\"\", {");

            if (DataSourceListToken != null)
            {
               sb.AppendLine("prePopulate: [");
               string strToken = "";
               foreach (DataRow dr in DataSourceListToken.Rows)
               {
                  strToken = (strToken == ""?"":strToken + ",") + "{id:\"" + dr["id"] + "\" , name: \"" + dr["name"] + "\"}";
               }
               sb.AppendLine(strToken);
               sb.AppendLine("],");
            }
                      
            sb.AppendLine("    theme: \"facebook\"");
            sb.AppendLine("    });");
            sb.AppendLine("});");

            ScriptManager.RegisterStartupScript(this, this.GetType(), "SDText" + this.ID, sb.ToString(), true);

        }

    }
}
